package cn.droidlover.xrecyclerview.demo.slice;

import cn.droidlover.xrecyclerview.XRecyclerView;
import cn.droidlover.xrecyclerview.demo.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class SampleItemProvider<T> extends XRecyclerView.RecyclerAdapter<T> {
    // ListContainer的数据集合
    private final List<String> settingList;
    private final Context context;

    public SampleItemProvider(Context context, List<T> data) {
        //Context context, List<T> data, int mLayoutId
        super(context,data);
        this.settingList = (List<String>) data;
        this.context=context;
    }
    // 用于保存列表项中的子组件信息
    private static class SampleItemHolder {

        Text settingText;
        Text text_noData;
        SampleItemHolder(Component component) {

            settingText = (Text) component.findComponentById(ResourceTable.Id_item_index);

            text_noData = (Text) component.findComponentById(ResourceTable.Id_text_noData);
        }

    }



    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        SampleItemHolder holder;
        String setting = settingList.get(position);
        if (component == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_sample, null, false);

            holder = new SampleItemHolder(cpt);
            // 将获取到的子组件信息绑定到列表项的实例中
            cpt.setTag(holder);
        } else {
            cpt = component;
            // 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
            holder = (SampleItemHolder) cpt.getTag();
        }
        if(position==14){
            //显示head布局
            holder.text_noData.setVisibility(Component.VISIBLE);
        }else{
            holder.text_noData.setVisibility(Component.HIDE);

        }
        holder.settingText.setText(setting);
        return cpt;
    }
}
